/*********************************************************************
* 
* S3PICPROG -  PIC programmer
* Copyright (C) 2008 Olivier Ringot <oringot@gmail.com>
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, 
* Boston, MA  02110-1301, USA.
* 
**********************************************************************
* 
* $Revision: $
* $Date: $
* $Source: $
* $Log: $
*
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include "icsp.h"

int main(int argc,char **argv)
{
  FILE *file_in;
  unsigned int i,opcode,data;
  unsigned char c;
  
  icsp_ttys=open("/dev/ttyS0",O_RDWR);
  if(icsp_ttys==-1)
  {
    printf("error: unable to open /dev/ttyS0\n");
    exit(-1);
  }
  
  /* parse opcode arguments */
  opcode = strtoul(argv[1],(char**)NULL,16);
  
  switch(opcode)
  {
    /* core_instr */
    case 0x10:
    case 0x30:
    case 0x31:
    case 0x80:
    case 0x81:
    case 0x82:
    case 0x83:
    case 0x90:
    
      if(argc!=4)
      {
        printf("error: bad argument number !\n");
        printf("usage: s3pic_cntl <0x%-.2x> <data0> <data1>\n",opcode&0xff);
        exit(-1);
      }
      
      send_char(opcode);
      
      data = strtoul(argv[2],(char**)NULL,16);
      send_char(data&0xff);
      data = strtoul(argv[3],(char**)NULL,16);
      send_char(data&0xff);
      
      check_ack();
      break;
      
      
    case 0x20:
    
      if(argc!=2)
      {
        printf("error: bad argument number !\n");
        printf("usage: s3pic_cntl <0x%-.2x>\n",opcode&0xff);
        exit(-1);
      }
      
      send_char(opcode);
      data=get_char();
      printf("%-.2x\n",data&0xff);
      check_ack();
      break;
  
    case 0x21:
    
      if(argc!=2)
      {
        printf("error: bad argument number !\n");
        printf("usage: s3pic_cntl <0x%-.2x>\n",opcode&0xff);
        exit(-1);
      }
      
      send_char(opcode);
      
      for(i=0;i<32;i++)
      {
        data=get_char();
        printf("%-.2x ",data&0xff);
        if(i==15)
        {
          printf("\n");
        }
      }  
      printf("\n");
      
      check_ack();
      break;
      
  
    case 0x32:
    
      if(argc!=34)
      {
        printf("error: bad argument number !\n");
        printf("usage: s3pic_cntl <0x%-.2x> <data0>..<data31>\n",opcode&0xff);
        exit(-1);
      }
      
      send_char(opcode);
      
      for(i=0;i<32;i++)
      {
        data=strtoul(argv[2+i],(char**)NULL,16);
        send_char(data&0xff);
      }  
      check_ack();
      break;
      
    default:
    
      printf("error: unimplemented opcode (0x%-.2x) !\n",opcode&0xff);
      exit(-1);
      
  }
  
  close(icsp_ttys);
}

